# -*- coding: utf-8 -*-
import scrapy
from qichacha.items import QichachaItem


class Qichacha1Spider(scrapy.Spider):
    name = 'qichacha1'
    allowed_domains = ['qichacha.com']
    start_urls = ['https://www.qichacha.com/g_HUB_420100/']

    def parse(self, response):
        # filename = "qichacha.html"
        # print(response.body);
        # open(filename, 'wb').write(response.body)

        items = []
        print("开始解析数据")
        tbody_content = response.xpath('//*[@id="searchlist"]/table/tbody/tr/td[2]')
        print("tbody---->")
        print(tbody_content)
        for each in tbody_content:
            item = QichachaItem()
            print("each---->")
            print(each)
            company = each.xpath('a/text()').extract()
            print("company---->")
            print(company)
            phone = each.xpath("p[2]/span/text()").extract()
            owner = each.xpath("p[1]/a/text()").extract()
            addr = each.xpath("p[3]/text()").extract()
            item['company'] = company[0]
            item['phone'] = phone[0]
            item['owner'] = owner[0]
            item['addr'] = addr[0]
            items.append(item)
        pass

        return items
